﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Sparkle.Hosting
{
    /// <summary>
    /// Interaction logic for HostingPanel.xaml
    /// </summary>
    public  class HostingPanel : UserControl
    {
        public HostingPanel()
        {
            
        }

        #region Attributes

        /// <summary>
        /// Gets or sets the content of a <see cref="T:System.Windows.Controls.ContentControl"/>. This is a dependency property.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// An object that contains the control's content. The default value is null.
        /// </returns>
        public new object Content
        {
            get { return base.Content; }
            set
            {
                base.Content = value as UIElement;
                var childControl = value as UserControl;
                if(childControl!=null)
                {
                    childControl.Loaded += ChildControlLoaded;
                }
            }
        }

        void ChildControlLoaded(object sender, RoutedEventArgs e)
        {
            UpdateLinkPanels(base.Content as UserControl);
        }


        #region LinkPanels

        private List<ILinkPanel> _linkPanels = new List<ILinkPanel>();
        /// <summary>
        /// LinkPanels
        /// </summary>
        public List<ILinkPanel> LinkPanels
        {
            get { return _linkPanels; }
            private set { _linkPanels = value; }
        }

        #endregion LinkPanels

        #endregion

        #region public private methods

        private void UpdateLinkPanels(UserControl content)
        {
            foreach (var linkpanel in _linkPanels)
            {
                linkpanel.PanelChanged(content);
            }
        }

        #endregion

        #region static methods and variables

        public static HostingPanel DefaultHostingPanel { get; set; }

        /// <summary>
        /// Notifies the changed.
        /// </summary>
        /// <param name="content">The content.</param>
        public static void NotifyChanged(UserControl content)
        {
            if (DefaultHostingPanel != null)
            {
                DefaultHostingPanel.UpdateLinkPanels(content);
            }
        }

        public static void HostControl(UserControl control)
        {
            if (DefaultHostingPanel != null)
            {
                DefaultHostingPanel.Content = control;
            }
        }

        public static UserControl HostContro
        {
            get
            {
                UserControl control = null;
                if (DefaultHostingPanel != null)
                {
                    control = DefaultHostingPanel.Content as UserControl;
                }
                return control;
            }
        }

        public static Object HostedEntity
        {
            get
            {
                object retObject = null;

                if (DefaultHostingPanel != null && (DefaultHostingPanel.Content is UserControl))
                {
                    retObject = (DefaultHostingPanel.Content as UserControl).DataContext;
                }

                return retObject;
            }
        }

        #endregion

    }
}
